﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TimViec.backend
{
    public partial class DoiTac : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(Session["Admin"]==null)
            {
                Response.Redirect("/quan-tri/login.html");
            }
            if(!IsPostBack)
            {
                LoadData();
            }
        }
        public void LoadData()
        {
            rptDoiTac.DataSource = BLL.Admin.DoiTacHelper.List_Doitac();
            rptDoiTac.DataBind();
        }
        private string UploadFile(FileUpload File)
        {
            //string Today = DateTime.Now.Year.ToString() + "/" + DateTime.Now.Month.ToString() + "/" + DateTime.Now.Day.ToString();
            string today = DateTime.Now.ToShortDateString();
            string UploadPath = "/UserFile/DoiTac/";
            string FilePath = UploadPath + DateTime.Now.ToShortDateString().Replace('/', '-') + DateTime.Now.ToLongTimeString().Replace(':', '-') + DateTime.Now.Millisecond + File.FileName;
            //string FilePath = UploadPath + File.FileName;

            /* N?u thu m?c t?n t?i */
            if (Directory.Exists(Server.MapPath(UploadPath)))
            {
                File.SaveAs(Server.MapPath(FilePath));
                //Utils.Help.CreateThumbnail(Server.MapPath(FilePath), 140, 105);
            }
            else
            {
                /* T?o thu m?c n?u chua t?n t?i */
                Directory.CreateDirectory(Server.MapPath(UploadPath));
                File.SaveAs(Server.MapPath(FilePath));
            }

            return FilePath;
        }
        protected void btSave_Click(object sender, EventArgs e)
        {
            string upfile = null;
            if (File_UpLoadImage.HasFile)
            {
                if (File_UpLoadImage.PostedFile.ContentLength > 1049000)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('File ảnh phải nhỏ hơn 1Mb!');", true);
                    return;
                }

                upfile = UploadFile(File_UpLoadImage);
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Chưa chọn logo đối tác!');", true);
                return;
            }
            BLL.Admin.DoiTacHelper.Add_DoiTac(txtName.Text,upfile,txtLink.Text);
            LoadData();
            txtLink.Text = "";
            txtName.Text = "";
        }

        protected void btUpdate_Click(object sender, EventArgs e)
        {
            string upfile = null;
            if (File_UpLoadImage.HasFile)
            {
                if (File_UpLoadImage.PostedFile.ContentLength > 1049000)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('File ảnh phải nhỏ hơn 1Mb!');", true);
                    return;
                }

                upfile = UploadFile(File_UpLoadImage);
            }            
            BLL.Admin.DoiTacHelper.Update_DoiTac(int.Parse(lbDoiTacId.Text),txtName.Text, upfile, txtLink.Text);
            LoadData();
            txtLink.Text = "";
            txtName.Text = "";
            btUpdate.Visible = false;
            btSave.Visible = true;
        }        
        protected void btCancel_Click(object sender, EventArgs e)
        {
            txtLink.Text = "";
            txtName.Text = "";
            btSave.Visible = true;
            btUpdate.Visible = false;
        }

        protected void rptDoiTac_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if(e.CommandName=="Edit")
            {
                int doitacid = int.Parse(e.CommandArgument.ToString());
                lbDoiTacId.Text = doitacid.ToString();
                DAL.tbl_DoiTac doiTac = BLL.Admin.DoiTacHelper.Get_DoiTacid(doitacid);
                if(doiTac!=null)
                {
                    txtName.Text = doiTac.Name;
                    txtLink.Text = doiTac.Link;
                    btUpdate.Visible = true;
                    btSave.Visible = false;
                }
            }
            if (e.CommandName == "Delete")
            {
                int doitacid = int.Parse(e.CommandArgument.ToString());
                BLL.Admin.DoiTacHelper.Delete_DoiTac(doitacid);
                LoadData();
                txtLink.Text = "";
                txtName.Text = "";
                btSave.Visible = true;
                btUpdate.Visible = false;
            }
        }
    }
}